﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_PlanManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from [Plan] where username='"+Session["UserName"].ToString()+"'";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        this.Label1.Text =" 共"+ds.Tables[0].Rows.Count+" 条记录";
        con.Close();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string PlanID = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        Response.Redirect("PlanInFo.aspx?planid="+PlanID+"");
    }
    protected void bnQuery_ServerClick(object sender, EventArgs e)
    {
        DateTime FD = Convert.ToDateTime(this.TextBox1.Text);
        DateTime TD = Convert.ToDateTime(this.TextBox2.Text);
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();

        string sql = "select * from [Plan] where AddTime between '" + FD + "' and '" + TD + "' and username='" + Session["UserName"].ToString() + "'";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "记录";
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string PlanID = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        Response.Redirect("MyPlan.aspx?PlanId="+PlanID+"");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int PlanId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "Delete from [Plan] where Planid="+PlanId+"";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        Response.Write("<script>alert('删除成功！')</script>");
        Bind();
        con.Close();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认要删除?')");
            }
        }
    }
}
